<?php
namespace Home\Controller;
use Think\Controller;
use Common\Model\CurrencyModel;
use Common\Model\UserMoneyModel;
use Common\Model\PriceModel;
use Common\Model\UserModel;
use Common\Model\TradeSellModel;
use Common\Model\TradeBuyModel;
use Common\Model\SplitModel;
class TradeController extends HomeController {
    protected $buy_currency;
    protected $sell_currency;
    protected $trade_user_money;
    public function __construct()
    {
        parent::__construct();
        $buy_currency_id=11;
        $sell_currency_id=1;
        
        $currency=CurrencyModel::newSelf()->getInfo();
        
        $this->buy_currency=$currency[$buy_currency_id];
        $this->sell_currency=$currency[$sell_currency_id];
        
        $this->assign('buy_currency',$currency[$buy_currency_id]);
        $this->assign('sell_currency',$currency[$sell_currency_id]);
        
        $this->trade_user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->assign('trade_user_money',$this->trade_user_money);
    }
    #登录首页
    public function index()
    {
        
        $price_list=PriceModel::newSelf()->getPriceList();
        $this->assign('price_list',$price_list);
        
        $now_price=PriceModel::newSelf()->findNowPrice();
        $this->assign('now_price',$now_price);
        
        $now_buy_price=PriceModel::newSelf()->findNowBuyPrice();
        $this->assign('now_buy_price',$now_buy_price);

        $now_sell_price=PriceModel::newSelf()->findNowSellPrice();
        $this->assign('now_sell_price',$now_sell_price);
        
        $this->display();
    }

//     public function index2()
//     {
//         $price=2;
//         while ($price<=5)
//         {
//             $data['price']=$price;
//             $data['num']=5000;
//             $data['status']=0;
//             M('Price')->add($data);
//             $price+=0.01;
//         }
//     }
    public function sellSub()
    {
        $this->eqValidate($this->config['CAN_SELL'], 1,'交易挂卖功能占时关闭');
        if ($this->config['MAX_SELL_DAY']>0) {
            $today_strtime=strtotime(date('Y-m-d'));
            $day_sell=TradeSellModel::newSelf()->where('add_time >= '.$today_strtime)->sum('num');
            $this->gtValidate($this->config['MAX_BUY_DAY'],$day_sell,'交易挂卖功能占时关闭');
        }
        
        $data['num']=(int)I('num');
        $data['twopassword']=I('twopassword');
        $data['price']=I('price');
        $this->notEmptyValidate($data['price'],'请输入卖出金额');
        $this->notEmptyValidate($data['num'],'请输入卖出数量');
        $this->notEmptyValidate($data['twopassword'],'请输入二级密码');
        $this->numVerification($data['num'],'请输入正确金额');
        $this->gtValidate($data['num'],0,'请输入正确金额');

        
        $res=UserModel::newSelf()->checkUserTwoPassword($data['twopassword'], $this->home_user_info['twopassword']);
        $this->trueValidate($res,'二级密码不正确');
        unset($data['twopassword']);

        $user_money=$this->trade_user_money[$this->sell_currency['id']]['num'];
        $this->eltValidate($data['num'],$user_money*$this->config['TRADE_RATIO_DAY'],'超出每日限定金额');
        $this->egtValidate($user_money,$data['num'],'您的'.$this->sell_currency['name'].'不足');
        
        $price_model=PriceModel::newSelf();
        $now_price=$price_model->findNowSellPrice();
        $this->egtValidate($data['price'],$now_price['price'],'您的价格必须大于等于当前出售价格');

        $price=$price_model->findPriceByPrice($data['price']);
        $this->notEmptyValidate($price,'请输入正确价格');
        $this->egtValidate($price['num']-$price['sell_num'],$data['num'],'超出最大出售数量');
        
        
        
        
//         $data['surplus_num']=$data['num'];
//         $data['user_id']=$this->home_user_info['id'];
//         $data['add_time']=time();
//         $data['status']=0;
//         UserMoneyModel::newSelf()->decUserMoney($this->home_user_info['id'], $this->sell_currency['id'], $data['num']);
        
        $price_model->where(['id'=>$price['id']])->setInc('sell_num',$data['num']);
        
//         $res=TradeSellModel::newSelf()->add($data);

        $res=UserMoneyModel::newSelf()->decUserMoney($this->home_user_info['id'], $this->sell_currency['id'],$data['num']);
        $res=$this->tradeSellCore($this->home_user_info['id'],$data['num'],$this->buy_currency,$this->sell_currency,$data['price']);
        
        $this->resReturn($res, '挂单成功', '系统繁忙请稍后再试');

        
    }
    public function buySub()
    {
        $this->eqValidate($this->config['CAN_SELL'], 1,'交易挂买功能占时关闭');
        $data['num']=(int)I('num');
        $data['twopassword']=I('twopassword');
        $this->notEmptyValidate($data['num'],'请输入转换金额');
        $this->notEmptyValidate($data['twopassword'],'请输入二级密码');
        $this->numVerification($data['num'],'请输入正确金额');
        $this->gtValidate($data['num'],0,'请输入正确金额');

        
        $res=UserModel::newSelf()->checkUserTwoPassword($data['twopassword'], $this->home_user_info['twopassword']);
        $this->trueValidate($res,'二级密码不正确');
        unset($data['twopassword']);

        $user_money=$this->trade_user_money[$this->buy_currency['id']]['num'];
        
        $price_model=PriceModel::newSelf();
        $now_price=$price_model->findNowBuyPrice();
        $this->egtValidate($user_money,$data['num']*$now_price['price'],'您的'.$this->buy_currency['name'].'不足');
        
        $this->egtValidate($now_price['num']-$now_price['buy_num'],$data['num'],'超出最大购买数量');

        $res=$this->tradeBuyBegin($this->home_user_info['id'],$data['num'],$this->buy_currency,$this->sell_currency);
        
        $this->resReturn($res, '购买成功', '系统繁忙请稍后再试');

        
    }
    public function tradeBuyLog()
    {
        $where['user_id']=$this->home_user_info['id'];
        $list=TradeBuyModel::newSelf()->pagingList($where);
        $this->assign('list',$list);
        $this->display();
    }
    public function tradeSellLog()
    {
        $where['user_id']=$this->home_user_info['id'];
        $list=TradeSellModel::newSelf()->pagingList($where);
        $this->assign('list',$list);
        $this->display();
    }
    public function splitLog()
    {
        $where['user_id']=$this->home_user_info['id'];
        
        $list=SplitModel::newSelf()->pagingList($where);
        
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
        $this->assign('list',$list);
        $this->display();
    }
    
    
    
    
    
    
    
    
    
    
    
}